package com.cxl.demo.service;

import com.cxl.demo.mapper.BuyHistoryMapper;
import com.cxl.demo.mapper.GoodsMapper;
import com.cxl.demo.mapper.LogisticsMapper;
import com.cxl.demo.mapper.UserMapper;
import com.cxl.demo.vo.BuyHistory;
import com.cxl.demo.vo.Goods;
import com.cxl.demo.vo.Pager;
import com.cxl.demo.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BuyHistoryServiceImpl implements BuyHistoryService{

    @Autowired
    UserMapper userMapper;
    @Autowired
    BuyHistoryMapper buyHistoryMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    LogisticsMapper logisticsMapper;



    @Override
    public Integer addBuyHistory(BuyHistory buyHistory) {
        Goods goods= goodsMapper.findGoodsById(buyHistory.getGoodsId());
        User user=userMapper.findUserByUserId(buyHistory.getBuyGoodsUserId());
        if((goods.getGoodsNumber()>buyHistory.getBuyGoodsNumber())&&(user.getScore()>(Double)(buyHistory.getBuyGoodsNumber()*goods.getGoodsScore()))){
            userMapper.updateSubScore(user.getUserId(),(Double)(buyHistory.getBuyGoodsNumber()*goods.getGoodsScore()));
            goodsMapper.updateGoodsSubNumber(goods.getGoodsId(),buyHistory.getBuyGoodsNumber());
            buyHistoryMapper.addBuyHistory(buyHistory);
            return 1;
        }else{

            System.out.println(goods.getGoodsNumber()>buyHistory.getBuyGoodsNumber());
            System.out.println(user.getScore()>(Double)(buyHistory.getBuyGoodsNumber()*goods.getGoodsScore()));

            System.out.println(goods);
            System.out.println(user);
            System.out.println(buyHistory);
            return 0;
        }

    }

    @Override
    public Integer delBuyHistory(Integer buyHistoryId) {
        BuyHistory buyHistory=buyHistoryMapper.findBuyHistoryById(buyHistoryId);




        if(buyHistory==null){
            return 0;
        }
        if(logisticsMapper.getHistoryNum(buyHistoryId)==0||logisticsMapper.getHistoryNum(buyHistoryId)==1){
            buyHistoryMapper.delBuyHistory(buyHistoryId);
            return 1;
        }
        return 0;
    }

    @Override
    public BuyHistory findBuyHistory(Integer buyHistoryId) {
        BuyHistory buyHistory=buyHistoryMapper.findBuyHistoryById(buyHistoryId);
        buyHistory.setBuyGoodsUser(userMapper.findUserInfoByUserId(buyHistory.getBuyGoodsUserId()));
        return    buyHistory;
    }

    @Override
    public Pager findUserBuyHistory(Map<String,Object> map) {

        List<BuyHistory> list=buyHistoryMapper.findUserBuyHistory(map);





        Pager pager=new Pager();
        pager.setRows(list);
        pager.setTotal(buyHistoryMapper.findUserBuyHistoryNum((Integer) map.get("buyGoodsUserId")));
        return pager;
    }

    @Override
    public Pager findGoodsBuyHistory(Map<String,Object> map) {
        List<BuyHistory> list=buyHistoryMapper.findGoodsBuyHistory(map);
        for(BuyHistory item:list){
            item.setBuyGoodsUser(userMapper.findUserInfoByUserId(item.getBuyGoodsUserId()));
        }
        Pager pager=new Pager();
        pager.setRows(list);
        pager.setTotal(buyHistoryMapper.findGoodsBuyHistoryNum((Integer) map.get("goodsId")));
        return pager;
    }


}
